#include<stdio.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int color[] = { 0x000000, 
                0xFFFFFF, 
                0xFF0000, 
                0x00FF00, 
                0x0000FF, 
                0xFFFF00, 
                0x00FFFF, 
                0xFF00FF
            };

int addr_list[] = { (0)*4, 
                    (800*240+0)*4, 
                    (800*240+200)*4, 
                    (800*240+400)*4, 
                    (800*240+600)*4, 
                    (600)*4, 
                    (400)*4,
                    (200)*4
                };

void show_block(char *addr, int color){
    for(int i = 0; i < 240; i++){
        for(int j = 0; j < 200; j++){
            memcpy(addr + (i*800 + j)*4, &color, 4);
        }
    }
}

void show(char *addr){
    int w = 200;
    int h = 240;
    int x = 0;
    while(1){
        for(int i = 0; i < 8; i++, x++){
            show_block(addr + addr_list[i], color[x%8]);
            printf("x = %d\n", x);
        }
        x++;
        x %= 8;
        sleep(1);
    }
}

int main(int argc, char const *argv[])
{
    //打开lcd
    int lcd = open("/dev/fb0", O_RDWR);
    if(lcd < 0){
        perror("lcd open error");
        return -1;
    }
    //映射
    char *addr = mmap(NULL, 800*480*4, PROT_WRITE, MAP_SHARED, lcd, 0);
    if(addr == MAP_FAILED){
        perror("mmap error");
        return -1;
    }
    //清屏
    memset(addr, 0, 800*480*4);
    show(addr);

    munmap(addr, 800*480*4);
    close(lcd);
    return 0;
}
